package com.MediaVaultTest;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import com.MediaVaultTest.PMF;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.apphosting.utils.remoteapi.RemoteApiPb.Request;

public class uploadThumbnail extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {

        Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
        BlobKey blobKey = blobs.get("myFile");

        if (blobKey == null) {
            res.sendRedirect("/");
        } else {
        	//Setup factoriess
        	 BlobInfo blobInfo;
        	 BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
        	 blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
        	 
        	//Add File to user
        	 PersistenceManager pm = PMF.get().getPersistenceManager();
        	 String stringMedia = req.getParameter("media");
        	 
        	 //Check size
        	 QuotaLogic quotaLogic = new QuotaLogic();
        	 if(blobInfo.getSize()> quotaLogic.getMaxThumbnailSize()||(blobInfo.getContentType().contains("image")==false)){
        		 	//Ohh rejected
        		   blobstoreService.delete(blobKey);
	         } else {
	        	 //Calculate data for object
	        	 Long Size = blobInfo.getSize();
	        	 String mediaType = blobInfo.getContentType();
	        	 String Title = blobInfo.getFilename();
	        	 
	    		 Key key = KeyFactory.stringToKey(stringMedia);
	    		 try { 
	    			 //Get the user store and create media file
	    			 Media media = pm.getObjectById(Media.class, key);
	    			 
	    			 //If no cover set, just add cover
	    			 if(media.getCoverForTrans()==""){
	    				 media.setCover(blobKey);
	    				 
	    			//Else if there was a previous cover
	    			 } else {
	    				 //Check to makue sure cover, is not file iteself
	    				 if(media.getCover()!=media.getFirstFormat().getBlobKey()){
	    					 blobstoreService.delete(media.getCover());
	    				 }
	    				 media.setCover(blobKey);
	    			 }
	    			 
	    		 } finally {
	    			 //Do nothing
	    		 }
        	 }
        	pm.close();
            res.sendRedirect("/viewInfo.jsp?key="+stringMedia);
        }
    }
}
